package son.vpoint.dinokids.until;

import java.util.LinkedList;

import org.anddev.andengine.util.MathUtils;

public class MathFactory {
	public static final int DIFF_EASY = 0;
	public static final int DIFF_NORMAL = 1;
	public static final int DIFF_HARD = 2;
	public static final int MAX_QUESTION_NUMBER = 10;
	public static final int OPERATOR_ADD = 0;
	public static final int OPERATOR_SUB = 1;
	public static final int OPERATOR_MULTI = 2;
	public static final int OPERATOR_DIVIS = 3;
	public static final int OPERATOR_MIX = 4;
	public static final int PARAM_ANSWER = -1;
	private static final String QUESTION_NO_TEMPLATE = "Question %d / %d";
	public static int mCurrentQuestionNo = 0;
	public static int mDifficultLevel = 0;
	public static int mOperatorMix=0;
	public 	static int mOperator = 0;
	private static int param1 = 0;
	private static int param2 = 0;
	private static int param3 = 0;
	private static int mResult = 0;

	
	public static LinkedList<Integer> conventionsCorrect(int paramInt) {
		LinkedList localLinkedList = new LinkedList();
		for (int i = 1; i < 10; i++) {

			if ((paramInt % i == 0) && (paramInt / i >= 1)
					&& (paramInt / i <= 9))
				localLinkedList.add(i);
		}
		return localLinkedList;
	}

	private void nextAddQuestion() {
		
		mOperatorMix=0;
		
		switch (mDifficultLevel) {
		

		case 0:

			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(2, 18);
				do {
					param1 = MathUtils.random(1, 9);
					param2 = (mResult - param1);
				} while ((param2 < 1) || (param2 > 9));
				if (MathUtils.random(0, 1) == 0)
					param1 = -1;
				else
					param2 = -1;
			} else {
			mDifficultLevel = 1;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 1:
			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(3, 17);
				param1 = -1;
				param2 = -1;
			} else {
				mDifficultLevel = 2;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 2:
			if (mCurrentQuestionNo == 10)
				break;
			mCurrentQuestionNo = (1 + mCurrentQuestionNo);
			mResult = MathUtils.random(4, 26);
			int i;
			do
				i = MathUtils.random(1, 9);
			while ((mResult - i > 17) || (mResult - i < 3));
			switch (MathUtils.random(1, 3)) {

			case 1:
				param1 = i;
				param2 = -1;
				param3 = -1;
				break;
			case 2:
				param1 = -1;
				param2 = i;
				param3 = -1;
				break;
			case 3:
				param1 = -1;
				param2 = -1;
				param3 = i;
				break;
			default:
				break;
			}


		}

	}

	private void nextSubQuestion() {
		mOperatorMix=0;

		switch (mDifficultLevel) {
		default:
			onGameWin();
		case 0:
			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(0, 8);
				do {
					param1 = MathUtils.random(1, 9);
					param2 = (param1 - mResult);
				} while ((param2 < 1) || (param2 > 9));
				if (MathUtils.random(0, 1) == 0)
					param1 = -1;
				else
					param2 = -1;
			} else {
				mDifficultLevel = 1;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 1:
			if (mCurrentQuestionNo != 10) {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
				mResult = MathUtils.random(1, 8);
				param1 = -1;
				param2 = -1;
			} else {
				mDifficultLevel = 2;
				mCurrentQuestionNo = 0;
				onLevelUp();
			}
			break;
		case 2:
			if (mCurrentQuestionNo == 10)
				break;
			mCurrentQuestionNo = (1 + mCurrentQuestionNo);
			switch (MathUtils.random(1, 3)) {

			case 1:
				int k = MathUtils.random(3, 9);
				mResult = MathUtils.random(0,  9 - k);
				param1 = (k + mResult);
				param2 = -1;
				param3 = -1;
				break;
			case 2:
				int j = MathUtils.random(1, 8);
				param2 = MathUtils.random(1,  j);
				mResult = (j - param2);
				param1 = -1;
				param3 = -1;
				break;
			case 3:
				int i = MathUtils.random(1, 8);
				param3 = MathUtils.random(1, i);
				mResult = (i - param3);
				param1 = -1;
				param2 = -1;
				break;

			}

		}

	}

	private void nextMulQuestion()
	 {
		mOperatorMix=0;

		
	 switch (mDifficultLevel)
	 {

	 case 0:
		 if (mCurrentQuestionNo != 10)
		 {
			mCurrentQuestionNo = (1 + mCurrentQuestionNo);
		 LinkedList localLinkedList;
		 do
		 {
		 mResult = MathUtils.random(1,  81);
		 localLinkedList = conventionsCorrect(mResult);
		 }
		 while (localLinkedList.size() == 0);
		 param1 = ((Integer) localLinkedList.get(MathUtils.random(0, -1 +
		 localLinkedList.size()))).intValue();
		 param2 = (mResult / param1);
		 if (MathUtils.random(0, 1) == 0)
		 param1 = -1;
		 else
		 param2 = -1;
		 }
		 else
		 {
		 mDifficultLevel = 1;
		 mCurrentQuestionNo = 0;
		 onLevelUp();
		 }
		 break;
	case 1:
		if(mCurrentQuestionNo !=10)
		{
			mCurrentQuestionNo =(1+mCurrentQuestionNo);
			LinkedList linkedList ;
			do{
			mResult = MathUtils.random(2, 72);
			linkedList = conventionsCorrect(mResult);
			}
			while(linkedList.size()==0);
			param1 =-1;
			param2 =-1;
			for(int k =0 ; k < linkedList.size() ; k++){
				int m =((Integer)linkedList.get(k)).intValue();
				if(mResult /m ==m)
					linkedList.remove(k);
			}
		
		}
		else
		{
			 mDifficultLevel = 2;
			 mCurrentQuestionNo = 0;
			 onLevelUp();
			 }
		 break;
	case 2:

		
		break;

	 }
	 }

	

	private void nextDivisQuestion() {
		mOperatorMix=0;

	
		switch (mDifficultLevel)
		 {

		 case 0:
			 if (mCurrentQuestionNo != 10)
			 {
				mCurrentQuestionNo = (1 + mCurrentQuestionNo);
			 LinkedList localLinked;
			 do
			 {
			 param1 = MathUtils.random(1,  81);
			 localLinked = conventionsCorrect(param1);
			 }
			 while (localLinked.size() == 0);
			 param2 = ((Integer) localLinked.get(MathUtils.random(0, -1 +
			 localLinked.size()))).intValue();
			 mResult = (param1 / param2);
			 if (MathUtils.random(0, 1) == 0)
			 param2 = -1;
			 else
			 mResult = -1;
			 }
			 else
			 {
			 mDifficultLevel = 1;
			 mCurrentQuestionNo = 0;
			 onLevelUp();
			 }
			 break;
		case 1:
			if(mCurrentQuestionNo !=10)
			{
				mCurrentQuestionNo =(1+mCurrentQuestionNo);
				LinkedList linkedList ;
				do{
				param1 = MathUtils.random(2, 72);
				linkedList = conventionsCorrect(param1);
				}
				while(linkedList.size()==0);
				mResult =-1;
				param2 =-1;
				for(int k =0 ; k < linkedList.size() ; k++){
					int m =((Integer)linkedList.get(k)).intValue();
					if(param1 /m ==m)
						linkedList.remove(k);
				}
			
			}
			else
			{
				 mDifficultLevel = 2;
				 mCurrentQuestionNo = 0;
				 onLevelUp();
				 }
			 break;	
		 }
	}
	private void nextMixQuestion() {
		switch (mDifficultLevel) {
		case 0:
			if(mCurrentQuestionNo !=10){
				mCurrentQuestionNo++;
				switch (MathUtils.random(1, 4)) 
				{
				case 1:
				
					mOperatorMix=1;
					mResult = MathUtils.random(2, 18);
					do {
						param1 = MathUtils.random(1, 9);
						param2 = (mResult - param1);
					} while ((param2 < 1) || (param2 > 9));
					if (MathUtils.random(0, 1) == 0)
						param1 = -1;
					else
						param2 = -1;
					break;
				case 2:
					mOperatorMix=2;
					mResult = MathUtils.random(0, 8);
					do {
						param1 = MathUtils.random(1, 9);
						param2 = (param1 - mResult);
					} while ((param2 < 1) || (param2 > 9));
					if (MathUtils.random(0, 1) == 0)
						param1 = -1;
					else
						param2 = -1;
					
					break;
				case 3:
					mOperatorMix=3;
					LinkedList localLinkedList;
					 do
					 {
					 mResult = MathUtils.random(1,  81);
					 localLinkedList = conventionsCorrect(mResult);
					 }
					 while (localLinkedList.size() == 0);
					 param1 = ((Integer) localLinkedList.get(MathUtils.random(0, -1 +
					 localLinkedList.size()))).intValue();
					 param2 = (mResult / param1);
					 if (MathUtils.random(0, 1) == 0)
					 param1 = -1;
					 else
					 param2 = -1;
					 
					
					break;
				case 4:
					mOperatorMix=4;
					LinkedList localLinked;
					 do
					 {
					 param1 = MathUtils.random(1,  81);
					 localLinked = conventionsCorrect(param1);
					 }
					 while (localLinked.size() == 0);
					 param2 = ((Integer) localLinked.get(MathUtils.random(0, -1 +
					 localLinked.size()))).intValue();
					 mResult = (param1 / param2);
					 if (MathUtils.random(0, 1) == 0)
					 param2 = -1;
					 else
					 mResult = -1;
					
					break;
				}
				}else {
					mDifficultLevel = 1;
						mCurrentQuestionNo = 0;
						onLevelUp();
					}
			break;	
			case 1:
				if (mCurrentQuestionNo != 10) 
				{
					mCurrentQuestionNo++;
					switch (MathUtils.random(1, 4)) 
					{
					case 1:
						mOperatorMix=1;
						mResult = MathUtils.random(3, 17);
						param1 = -1;
						param2 = -1;
						break;
					case 2:
						mOperatorMix=2;
						mResult = MathUtils.random(1, 8);
						param1 = -1;
						param2 = -1;
						break;
					case 3:
						mOperatorMix=3;
						LinkedList linkedList ;
						do{
						mResult = MathUtils.random(2, 72);
						linkedList = conventionsCorrect(mResult);
						}
						while(linkedList.size()==0);
						param1 =-1;
						param2 =-1;
						for(int k =0 ; k < linkedList.size() ; k++){
							int m =((Integer)linkedList.get(k)).intValue();
							if(mResult /m ==m)
								linkedList.remove(k);
						}
						break;
					case 4:
						mOperatorMix=4;
						LinkedList linkedList2;
						do{
						param1 = MathUtils.random(2, 72);
						linkedList2 = conventionsCorrect(param1);
						}
						while(linkedList2.size()==0);
						mResult =-1;
						param2 =-1;
						for(int k =0 ; k < linkedList2.size() ; k++){
							int m =((Integer)linkedList2.get(k)).intValue();
							if(param1 /m ==m)
								linkedList2.remove(k);
						}
						break;

					}

				} 
				else {
					mDifficultLevel = 2;
					mCurrentQuestionNo = 0;
					onLevelUp();
				}
				
				break;
			case 2:
			if(mCurrentQuestionNo ==10)
				break;
				
			
				
			}



				
		
	}
	private void onLevelUp() {

	}

	private void onGameWin() {

	}

	public String getQuestionNoString() {
		Object[] arrayOfObject = new Object[2];
		arrayOfObject[0] = Integer.valueOf(this.mCurrentQuestionNo);
		arrayOfObject[1] = Integer.valueOf(10);
		return String.format("Question %d / %d", arrayOfObject);
	}
	public int getDifficultLevel(){
		return mDifficultLevel;
	}
	public int getParam1()
	  {
	    return param1;
	  }

	  public int getParam2()
	  {
	    return param2;
	  }

	  public int getParam3()
	  {
	    return param3;
	  }
	  public int getResult()
	  {
	    return mResult;
	  }
	public void nextQuestion(){
		switch (mOperator) {
		case 0:
			nextAddQuestion();
			break;
		case 1:
			nextSubQuestion();
			break;
		case 2:
			nextMulQuestion();
			break;
		case 3:
			nextDivisQuestion();
			break;
		case 4:
			nextMixQuestion();
			break;

		default:
			break;
		}
	}


}
